package main

import (
	"flag"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
	"go.uber.org/fx"
	"zero/pkg/base"
	"zero/protos/user"
	"zero/rpc/user/internal/di"
	server "zero/rpc/user/internal/server/user"

	"zero/rpc/user/internal/config"
	"zero/rpc/user/internal/svc"

	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/core/service"
	"github.com/zeromicro/go-zero/zrpc"
	"google.golang.org/grpc"
	"google.golang.org/grpc/reflection"
)

var configFile = flag.String("f", "etc/gateway.yaml", "the config file")

func init() {
	logx.DisableStat()
}
func initConfig() (string, error) {
	nacosCfg, _ := base.NewNacosConfigurator(
		"192.168.6.98",
		"config",
		"zo",
		"zo",
	)
	rs, err := nacosCfg.GetConfig()
	return rs, err
}
func main() {
	flag.Parse()
	var c config.Config
	configStr, _ := initConfig()
	conf.LoadFromYamlBytes([]byte(configStr), &c)

	app := fx.New(
		di.Logical,
		di.Server,
		fx.Provide(func() config.Config { return c }),
		fx.Provide(svc.NewServiceContext),
		fx.Invoke(func(userSvc *server.UserServer) {
			s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
				user.RegisterUserServer(grpcServer, userSvc)
				//服务注册
				if c.Mode == service.DevMode || c.Mode == service.TestMode {
					reflection.Register(grpcServer)
				}
			})
			defer s.Stop()

			fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)
			s.Start()
		}),
	)

	app.Run()
}
